package com.gogowan.agroenterprise.newview;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.barcode.Intents;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.RetrofitUtil.GsonUtil;
import com.gogowan.agroenterprise.model.DealerInStorageRequest;
import com.gogowan.agroenterprise.model.IntStorageRequest;
import com.gogowan.agroenterprise.model.JX_Rk_Request;
import com.gogowan.agroenterprise.model.New_JX_Rk_Result_Bean;
import com.gogowan.agroenterprise.model.QueryJxRkHttpBean;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.utils.Util;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.Base_Adapater;

import java.util.ArrayList;
import java.util.List;

import u.aly.Q;

public class New_JX_Rk_Activity extends BaseListView {

    private List<String> list=new ArrayList<>();
    // 扫码返回的list
    private ArrayList<String> resultStringList=new ArrayList<>();
    private ArrayList<New_JX_Rk_Result_Bean.BoxInfoBean> boxInfoBeans=new ArrayList<>();
    private Base_Adapater adapter;
    private EditText editText;
    private String codeStr="";
    private ProgressDialog progressDialog;

    @Override
    protected void InitValue() {
        adapter=new Base_Adapater(this,boxInfoBeans);
        adapter.setOnItemListener(new Base_Adapater.OnItemListener() {
            @Override
            public String ItemStr(int position) {
                New_JX_Rk_Result_Bean.BoxInfoBean infoBean= boxInfoBeans.get(position);
                String val="码号:"+infoBean.getCode()+
                        "\n产品名称:"+infoBean.getMaterialName()+
                        "\n产品型号:"+infoBean.getMaterialSpecication()+
                        "\n产品状态:"+(infoBean.getUsed()>0?Utils.Httpblue(true,"正常"):Utils.HttpRed(true,"无法入库"))+
                        "\n入库数量:"+infoBean.getMaCount()+
                        "\n入库垛数:"+infoBean.getDuoCount()+
                        "\n入库箱数:"+infoBean.getBoxCount();
                return val;
            }
        });
        getListView().setAdapter(adapter);

    }

    @Override
    protected void InitView() {
        getHeadLayout().setOrientation(LinearLayout.VERTICAL);
        getFootLayout().setOrientation(LinearLayout.VERTICAL);
        SetHeadView(false,R.layout.new_scan_head_view);
        SetFootView(false,R.layout.layout_one_button);

        TextView HeadNameTv=(TextView)getHeadView().findViewById(R.id.sacn_head_name_tv);
        editText=(EditText) getHeadView().findViewById(R.id.scan_head_ed);
        getHeadView().findViewById(R.id.scan_head_scan_im).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intents.Scan.ACTION);
                intent.putExtra(Intents.Scan.IS_BULT_MODE, false);
                startActivityForResult(intent, 19);
            }
        });
        getHeadView().findViewById(R.id.scan_head_camera_list_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isEmpty(codeStr)){
                    showToast("请扫描单据码");
                    return;
                }
                Intent intent = new Intent(Intents.Scan.ACTION);
                intent.putExtra(Intents.Scan.IS_BULT_MODE, true);
                startActivityForResult(intent, 20);
            }
        });
        getHeadView().findViewById(R.id.scan_head_red_list_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isEmpty(codeStr)){
                    showToast("请扫描单据码");
                    return;
                }
                Intent intent = new Intent(New_JX_Rk_Activity.this, HWDialogActivity.class);
                intent.putExtra("type", "lx");
                startActivityForResult(intent, 505);
            }
        });
        HeadNameTv.setText("出库单号:");
        getFootView().findViewById(R.id.in_storage_qrrk_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InStorageHttp();
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                codeStr=editText.getText().toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void InStorageHttp() {
        JX_Rk_Request request=new JX_Rk_Request();
//        request.setDealerId(getLogInfo().getEnterpriseinfo().getDealerId());
//        request.setUserId(getLogInfo().getUserinfo()
//                .getPersonid());
//        request.setEnterpriseId(getLogInfo().getEnterpriseinfo().getEnterpriseid());
        request.setOutStorageID(editText.getText().toString());
        progressDialog = ProgressDialog.show(this, getString(R.string.alert_title),
                getString(R.string.alert_message), true, false);
        Http(request, "50000", progressDialog, new RetrofitHttpListener() {
            @Override
            public void OnJson(String str) {
                ResultInfo resultInfo=GsonUtil.GsonToBean(str,ResultInfo.class);
                if (resultInfo!=null){
                    showToast(resultInfo.getResultinfo());
                   if (resultInfo.getResult()>0){
                       boxInfoBeans.clear();
                       adapter.notifyDataSetChanged();
                       editText.setText("");
                       codeStr="";
                   }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode){
                case 19:
                    // 扫码获取单号
                    codeStr = data.getStringExtra(Intents.Scan.RESULT);
                    editText.setText(codeStr);
                    break;
                case 20:
                case 505:
                    resultStringList = data.getStringArrayListExtra(Intents.Scan.RESULTLIST);
                    if (resultStringList == null || resultStringList.size() == 0) {
                        return;
                    }
//                    adapter.notifyDataSetChanged();
                    for (int i=0;i<resultStringList.size();i++){
                        String result = resultStringList.get(i);
                        result=result.replaceAll("\n","");
                        String[] parts = result.split("MA.156.M0");
                        if (parts.length >= 3) {//"包含2个\"MA.156.M0\""
                            //
                            for (int j = 0; j < parts.length; j++) {
                                if (j==1){
                                    if (!resultStringList.contains("MA.156.M0"+parts[1])) {
                                        resultStringList.set(i, "MA.156.M0" + parts[1]);
                                    }
                                }else if(j>1){
                                    if (!resultStringList.contains("MA.156.M0"+parts[j])) {
                                        resultStringList.add(i + j + 1 - 2, "MA.156.M0" + parts[j]);
                                    }
                                }
                            }
                        }

                    }

                    QueryOneStringHttp();
                    break;

            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        QueryOneStringHttp();
    }

    private void QueryOneStringHttp() {
        if (resultStringList.size()<1){
            return;
        }
        progressDialog = ProgressDialog.show(this, getString(R.string.alert_title),
                getString(R.string.alert_message), true, false);
        QueryJxRkHttpBean httpBean=new QueryJxRkHttpBean();
//        httpBean.setEnterpriseId(getLogInfo().getEnterpriseinfo().getEnterpriseid());
//        httpBean.setDealerId(getLogInfo().getEnterpriseinfo().getDealerId());
        httpBean.setOutStorageNo(codeStr);
        httpBean.setBoxCode(resultStringList);
//        httpBean.setUserId(getLogInfo().getUserinfo()
//                .getPersonid());
        Http(httpBean, "49000", progressDialog, new RetrofitHttpListener() {
            @Override
            public void OnJson(String str) {
                showToast(str);
                if (!Utils.isEmpty(str)){
                    New_JX_Rk_Result_Bean rk_result_bean= GsonUtil.GsonToBean(str,New_JX_Rk_Result_Bean.class);
                    showToast(rk_result_bean.getResultinfo());
                    if (rk_result_bean.getResult()>0){
                        boxInfoBeans.clear();
                        boxInfoBeans.addAll(rk_result_bean.getBoxInfo());
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog!=null){
            progressDialog.dismiss();
        }
    }
}
